<%@ page language="java" import="com.weaver.general.BaseBean" pageEncoding="UTF-8" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@ page import="org.jsoup.Jsoup" %>
<%@ page import="org.jsoup.nodes.Document" %>
<%@ page import="org.jsoup.nodes.Element" %>
<%@ page import="org.jsoup.select.Elements" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.docs.docs.DocManager" %>
<%@ page import="weaver.docs.docs.DocViewer" %>
<%@ page import="weaver.docs.webservices.DocAttachment" %>
<%@ page import="weaver.docs.webservices.DocInfo" %>
<%@ page import="weaver.docs.webservices.DocServiceImpl" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%
    String basePath = "/usr/weaver/ecology/tempfile/xxjl/";
    User user = HrmUserVarify.getUser( request, response );
    out.print( ">>>>>>>开始同步文档>>>>>>><br/>" );
    String docids = init(basePath,user);
    out.print( ">>>>>>>同步文档结束>>>>>>>"+docids+"<br/>" );
%>

<%!
    public static String init(String basePath,User user) throws Exception{
        File[] list = new File( basePath ).listFiles();
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( ">>>>>>>>init>>>>>>>>>>");
        RecordSet rs = new RecordSet();
        String docids = "";
        int catelogid = 1021;//虚拟目录
        int secid = 3541;
        for (File file : list) {
            if (!file.isDirectory()) {
                baseBean.writeLog( "path="+file.getPath() );
                Map<Map<String, String>, List<Map<String, String>>> resultMap = getHref( file.getPath(), basePath );
                for (Map<String, String> key : resultMap.keySet()) {
                    int docid = -1;
                    List<Map<String, String>> mapList = resultMap.get( key );
                    String dateTime = key.get( "dateTime" );
                    String date = converDateChinese( dateTime.substring( 0, 10 ) );
                    String time = dateTime.substring( 11 );
                    docid = createDocs( user.getUID() + "", mapList, key ,secid,date,time);
                    baseBean.writeLog( ">>>>>docid="+docid );
                    if (docid > 0) {
                        rs.executeUpdate( "update docdetail set doccreatedate=?,doccreatetime=?," +
                                "docapprovedate=?,docapprovetime=? where id=?", date, time, date, time, docid );

                        rs.executeUpdate( "insert into docdummydetail(catelogid,docid) values('"+catelogid+"','"+docid+"')");
                        DocManager dm = new DocManager();
                        dm.setId( docid );
                        dm.setSeccategory( secid );
                        dm.setUserid( user.getUID() );
                        dm.setDocCreaterType( user.getLogintype() );
                        dm.AddShareInfo();
                        DocViewer DocViewer = new DocViewer();
                        DocViewer.setDocShareByDoc( docid + "" );
                    }
                    docids +="".equals( docids )?docid+"":","+docid;
                }
            }

        }
        return docids;
    }
    public static Map<Map<String, String>, List<Map<String, String>>> getHref(String path, String basePath) {
        Map<Map<String, String>, List<Map<String, String>>> listMap = new HashMap<>();
        List<Map<String, String>> mapList = new ArrayList<>();
        Map<String, String> mapData = new HashMap<>();
        BaseBean baseBean = new BaseBean();
        try {
            Document document = Jsoup.parse( new File( path ), "GBK" );
            String dateTime = document.getElementsByTag( "tr" ).get( 2 ).select( "td" ).last().text();
            String docsubject = document.getElementsByTag( "tr" ).first().select( "td" ).last().text();
            String content = document.getElementsByTag( "tr" ).get( 3 ).select( "td" ).last().html();
            Elements elements = document.getElementsByTag( "tr" ).get( 4 ).select( "a" );
            for (int i = 0; i < elements.size(); i++) {
                Map<String, String> map = new HashMap<>();
                Element element = elements.get( i );
                map.put( "rawFileName", element.attr( "href" ).substring( 37 ) );
                map.put( "path", basePath + element.attr( "href" ) );
                baseBean.writeLog( "map="+map.toString() );
                mapList.add( map );
            }
            mapData.put( "docsubject", docsubject );
            mapData.put( "dateTime", dateTime );
            mapData.put( "content", content );
            listMap.put( mapData, mapList );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listMap;
    }

    //创建文档
    public static int createDocs(String userid, List<Map<String, String>> list, Map<String, String> map,int secid,String date,String time) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>>createDocs>>>>>>");
        int docid = -1;
        DocServiceImpl docserviceimpl = new DocServiceImpl();
        DocInfo doc = new DocInfo(); //创建文档
        String keyword = ""; //文档内容
        try {
            baseBean.writeLog( ">>>>>size=" + list.size() );
            DocAttachment[] docattachment = new DocAttachment[list.size()];
            for (int i = 0; i < list.size(); i++) {
                baseBean.writeLog( ">>>>>i=" + i );
                Map<String, String> datamap = list.get( i );
                String rawFileName = Util.null2String( datamap.get( "rawFileName" ) );
                baseBean.writeLog( ">>>>>rawFileName=" + rawFileName );
                String filetype = "";
                if (rawFileName.lastIndexOf( "." ) > -1) {
                    filetype = rawFileName.substring( rawFileName.lastIndexOf( "." ) + 1 );
                }
                byte[] content = new byte[102400];
                // 上传附件，创建html文档
                content = null;
                try {
                    int byteread;
                    byte data[] = new byte[1024];
                    String path = datamap.get( "path" );
                    baseBean.writeLog(">>>>>path="+path);
                    File file = new File(path);
                    baseBean.writeLog(">>>>>file="+file);
                    InputStream input = new FileInputStream( file );
                    baseBean.writeLog(">>>>>input=");
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    baseBean.writeLog(">>>>>outputStream=");
                    while ((byteread = input.read( data )) != -1) {
                        outputStream.write( data, 0, byteread );
                        outputStream.flush();
                    }
                    baseBean.writeLog(">>>>>999999999");
                    content = outputStream.toByteArray();
                    baseBean.writeLog(">>>>>content="+content.toString());
                    input.close();
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                DocAttachment da = new DocAttachment();
                da.setFilename( rawFileName );//附件标题
                da.setFilecontent( Base64.encode( content ) ); //附件内容
                da.setFiletype( filetype ); //imagefiletype
                da.setImagefilename( rawFileName );// 附件标题
                da.setIszip( 1 );//是否压缩
                da.setIsAesEncrype( 0 );//是否加密
                da.setAesCode( "0" );
                da.setIsextfile( "1" ); //附件是否是office类型

                if (filetype.equalsIgnoreCase( "doc" )) {
                    da.setDocfiletype( "3" );
                } else if (filetype.equalsIgnoreCase( "xls" )) {
                    da.setDocfiletype( "4" );
                } else if (filetype.equalsIgnoreCase( "ppt" )) {
                    da.setDocfiletype( "5" );
                } else if (filetype.equalsIgnoreCase( "wps" )) {
                    da.setDocfiletype( "6" );
                } else if (filetype.equalsIgnoreCase( "docx" )) {
                    da.setDocfiletype( "7" );
                } else if (filetype.equalsIgnoreCase( "xlsx" )) {
                    da.setDocfiletype( "8" );
                } else if (filetype.equalsIgnoreCase( "pptx" )) {
                    da.setDocfiletype( "9" );
                } else {
                    da.setDocfiletype( "2" );
                }
                docattachment[i] = da;
                keyword = rawFileName;
                doc.setAttachments( docattachment );
            }

            doc.setSeccategory( secid );//子目录id
            doc.setDocSubject( map.get( "docsubject" ) );//标题
            doc.setDoccontent( map.get( "content" ) );//文档内容
            /*文档属性*/
            doc.setDocType( 1 );
            //文档状态
            doc.setDocStatus( 1 );
            doc.setDocStatusStr( "正常" );
            //新闻类型
            doc.getDocPublishType();
            doc.setPublishable( "文档" );

            doc.setKeyword( keyword );
            //创建
            doc.setDoccreaterid( Util.getIntValue(userid) );
            doc.setDoccreatertype( 1 );
            doc.setDoccreatername( "系统管理员" );
            doc.setDoccreatetime( time );
            doc.setDoccreatedate( date );
            //关联列表
            doc.setChilddoc( new int[]{} );
            doc.setChiledocname( new String[]{} );
            //文档所有者
            doc.setOwnerid(Util.getIntValue(userid) );
            doc.setOwnertype( 1 );
            doc.setOwnername( "系统管理员" );
            if (!"".equals( userid )) {
                User user = new User( Util.getIntValue( userid ) );
                docid = docserviceimpl.createDocByUser( doc, user );
                baseBean.writeLog( ">>>>>docid="+docid );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return docid;
    }

    public static String converDateChinese(String date) {
        String newDate = "";
        if (date.contains( "/" )) {
            String b1 = date.replace( "/", "-" );
            String b2 = b1.replace( "/", "-" );
            newDate = b2.replace( "/", " " ).trim();
        } else {
            newDate = date;
        }
        return newDate;
    }
%>